// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var base = require("base");
	var router = require("../hash/pageRouter");
	var allInterval = [];
	var ws;
	var storage = window.sessionStorage;
	
	var init = function() {
		initEvent();
		router.init();
	};
	var initEvent = function() {
		//		登陆
		$("#content").on("click", "#platform_login", function() {
			checkUser();
		});
		
		document.onkeydown = function(event){
			if(13 == event.keyCode){
				checkUser();
			}
		}
		
		$("#content").on("click", ".refresh", function(){
			clearAllInterval();
			$(this).addClass("none");
			$("#jStatic").removeClass("none");
			$(".jStatic").addClass("none");
			$(".jQuick").removeClass("none");
			getRandomCode();
		});
		
		$("#jStatic").unbind("click").click(function(){
			clearAllInterval();
			$(this).addClass("none");
			$("#jQuick").removeClass("none");
			$(".jQuick").addClass("none");
			$(".jStatic").removeClass("none");
		});
		
		$("#jQuick").unbind("click").click(function(){
			clearAllInterval();
			$(this).addClass("none");
			$("#jStatic").removeClass("none");
			$(".jStatic").addClass("none");
			$(".jQuick").removeClass("none");
			getRandomCode();
		});
		
		
	};

	//  登陆方法
	var checkUser = function() {
		var mUsername = $('#platform_username').val().trim();
		var mPassword = $('#platform_password').val().trim();
		if (mUsername == "") {
			$('.error_info').css('visibility', 'visible');
			$('.error_info').html("请输入用户名")
		} else if (mPassword == "") {
			$('.error_info').css('visibility', 'visible');
			$('.error_info').html("请输入密码")
		} else {
			base.fnAjax({
				action: "userNameLogin.json",
				version: "v3.0",
				async: false,
				nocode: true,
				oData: {
					username: mUsername,
					password: mPassword,
					appKey: "FINANCE-PLATFORM-WEB"
				}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						base.saveData("sid", data.datas[0].sessionUuid);
						//存sessionUuid
						base.saveData("appKey", "FINANCE-PLATFORM-WEB");
						
						storage.removeItem("firstTimeVisit");
						//存appkey
						base.openUrl("../main/platform_main.html");
						//存账号
						if (mUsername.indexOf('@') == -1) {
							base.saveData("userName", mUsername);
						} else {
							base.saveData("userName", mUsername.substr(0, mUsername.indexOf('@')));
						}
					} else {
						$('.error_info').css('visibility', 'visible');
						$('.error_info').html(data.message);
					}
				}
			});
		}
	};
	
	var getRandomCode = function(){
		base.fnAjax({
			action: "getRandomCode.json",
			async: false,
			nocode: true,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var rd = data.datas[0].randomCode;
					$("#qrcode").html("");
					$("#qrcode").qrcode({width:200, height:200, text: "randomCode=" + rd});
					var startTime = new Date().getTime();
					var interval = setInterval(function(){
						if(new Date().getTime() - startTime > 120000){
							clearInterval(interval);
							var str = '<div class="msg-err"><h6>二维码已失效</h6><a class="refresh J_QRCodeRefresh" href="javascript:;">请点击刷新</a></div>';
							$("#qrcode").append(str);
						}
						getSid(rd);
					}, 5000);
					allInterval.push(interval);
				} else {
					$('.error_info').css('visibility', 'visible');
					$('.error_info').html(data.message);
				}
			}
		});
	};
	
	var getSid = function(randomCode){
		base.fnAjax({
			action: "getSid.json",
			async: false,
			nocode: true,
			oData: {
				randomCode: randomCode
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0] != null) {
						base.saveData("sid", data.datas[0].sessionUuid);
						//存sessionUuid
						base.saveData("appKey", "FINANCE-PLATFORM-WEB");
						//存appkey
						base.openUrl("../main/platform_main.html");
					}
				} else {
					$('.error_info').css('visibility', 'visible');
					$('.error_info').html(data.message);
				}
			}
		});
	};
	
	var clearAllInterval = function() {
		$.each(allInterval, function() {
			clearInterval(this);
		});
	}
	
	return {
		init: init
	};
});